package com.example.stringbufferlogger.Utils;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class RemoteCallUtil {

    private static final RestTemplate restTemplate = new RestTemplate();

    /**
     * 调用远程接口
     *
     * @param url       远程接口地址
     * @param params    请求参数
     * @param method    请求方法 (GET/POST)
     * @return          响应结果
     */
    public static String callRemoteApi(String url, Map<String, String> params, String method) {
        try {
            if ("GET".equalsIgnoreCase(method)) {
                return restTemplate.getForObject(url, String.class, params);
            } else if ("POST".equalsIgnoreCase(method)) {
                return restTemplate.postForObject(url, params, String.class);
            } else {
                throw new IllegalArgumentException("请求失败: " + method);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    public static Map<String, Object> callRemote(String url, Object requestBody, String method) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Map> response = restTemplate.postForEntity(url, requestBody, Map.class);
        return response.getBody();
    }


}